<template>
  <!-- 人员事故 -->
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="auto" size="small">
      <el-form-item label="人员名称" prop="employeeId">
        <el-select v-model="queryParams.employeeId" clearable filterable placeholder="请搜索选择从业人员" style="width: 217px">
          <el-option
            v-for="dict in personnel"
            :key="dict.employeeId"
            :label="dict.name"
            :value="dict.employeeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="事故发生时间" prop="accidentOccurTime">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="-"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd HH:mm"
        />
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:accident:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['system:accident:export']"-->
      <!--          icon="el-icon-download"-->
      <!--          plain-->
      <!--          size="mini"-->
      <!--          type="warning"-->
      <!--          @click="handleExport"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="accidentList" @selection-change="handleSelectionChange">
      <el-table-column align="center" label="事故发生时间" prop="accidentOccurTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.accidentOccurTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="事故上报时间" prop="accidentEscalationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.accidentEscalationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="人员" prop="name" />
      <el-table-column align="center" label="证件编号" prop="certCode" />
      <el-table-column align="center" label="事故编号" prop="accidentCode" />
      <el-table-column :show-overflow-tooltip="true" align="center" label="事故名称" prop="accidentName" />
      <el-table-column align="center" label="本次扣分" prop="score" />
      <el-table-column :show-overflow-tooltip="true" align="center" label="事故详情" prop="accidentDetails" />
      <el-table-column :show-overflow-tooltip="true" align="center" label="事故处置结果" prop="accidentResult" />
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleexamine(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改人员事故对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="姓名" prop="employeeId">
          <el-select v-model="form.employeeId" :disabled="disabledon" clearable filterable placeholder="请搜索选择姓名" style="width: 100%">
            <el-option
              v-for="dict in personnel"
              :key="dict.employeeId"
              :label="dict.name"
              :value="dict.employeeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="事故编号" prop="accidentCode">
          <el-input v-model="form.accidentCode" :disabled="disabledon" placeholder="请输入事故编号" style="width: 100%" />
        </el-form-item>
        <el-form-item label="事故名称" prop="accidentName">
          <el-input v-model="form.accidentName" :disabled="disabledon" placeholder="请输入事故名称" style="width: 100%" />
        </el-form-item>
        <el-form-item label="事故发生时间" prop="accidentOccurTime">
          <el-date-picker
            v-model="form.accidentOccurTime"
            clearable
            placeholder="请选择事故发生时间"
            style="width: 100%"
            type="datetime"
            :disabled="disabledon"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="事故上报时间" prop="accidentEscalationTime">
          <el-date-picker
            v-model="form.accidentEscalationTime"
            clearable
            placeholder="请选择事故上报时间"
            style="width: 100%"
            type="datetime"
            :disabled="disabledon"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="本次扣分" prop="score">
          <el-input-number v-model="form.score" :disabled="disabledon" :min="0" :step="2" style="width: 100%" />
        </el-form-item>
        <el-form-item label="事故详情" prop="accidentDetails">
          <el-input
            v-model="form.accidentDetails"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入事故详情"
            style="width: 100%"
            :disabled="disabledon"
            type="textarea"
          />
        </el-form-item>
        <el-form-item label="事故处置结果" prop="accidentResult">
          <el-input
            v-model="form.accidentResult"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入事故处置结果"
            style="width: 100%"
            :disabled="disabledon"
            type="textarea"
          />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!disabledon" v-no-more-click type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { listAccident, getAccident, delAccident, addAccident, updateAccident } from '@/api/bhxq/accident'
// import { getpersonnel } from '../../../api/bhxq/aptitude'
import { parseTime } from '../../../utils/ruoyi'
import {
  addAccident,
  delAccident,
  getAccident,
  getpersonnel,
  listAccident,
  updateAccident
} from '@/api/informationReport'

export default {
  name: 'Index',
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 日期范围
      dateRange: [],
      personnel: [],
      disabledon: true,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员事故表格数据
      accidentList: [{}],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
        employeeId: null,
        // businessType: null,
        accidentCode: null,
        accidentName: null,
        accidentOccurTime: null,
        accidentEscalationTime: null,
        accidentDetails: null,
        score: null,
        accidentResult: null,
        postStatus: null
      },
      queryParamslist: {
        employeeId: null
        // businessType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        enterpriseId: [
          { required: true, message: '企业ID不能为空', trigger: 'change' }
        ],
        employeeId: [
          { required: true, message: '姓名不能为空', trigger: 'change' }
        ],
        businessType: [
          { required: true, message: '类型不能为空', trigger: 'change' }
        ],
        accidentCode: [
          { required: true, message: '事故编号不能为空', trigger: 'blur' }
        ],
        accidentName: [
          { required: true, message: '事故名称不能为空', trigger: 'blur' }
        ],
        accidentOccurTime: [
          { required: true, message: '事故发生时间不能为空', trigger: 'blur' }
        ],
        accidentEscalationTime: [
          { required: true, message: '事故上报时间不能为空', trigger: 'blur' }
        ],
        // accidentDetails: [
        //   { required: true, message: '事故详情不能为空', trigger: 'blur' }
        // ],
        score: [
          { required: true, message: '扣分不能为空', trigger: 'blur' }
        ]
        // accidentResult: [
        //   { required: true, message: '事故处置结果不能为空', trigger: 'blur' }
        // ]
      }
    }
  },
  created() {
    this.getList()
    this.getpersonnellist()
  },
  methods: {
    parseTime,
    // 查询人员列表
    getpersonnellist() {
      // this.queryParamslist.businessType = 1
      getpersonnel(this.queryParamslist).then(response => {
        this.personnel = response.data
      })
    },
    /** 查询人员事故列表 */
    getList() {
      this.loading = true
      // this.queryParams.businessType = 1
      if (this.dateRange == null) {
        this.dateRange = []
      }
      const data = {
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
        employeeId: this.queryParams.employeeId,
        accidentOccurTimeStart: this.dateRange.length === 0 ? undefined : this.dateRange[0],
        accidentOccurTimeEnd: this.dateRange.length === 0 ? undefined : this.dateRange[1]
      }
      listAccident(data).then(response => {
        this.accidentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        // id: null,
        // enterpriseId: null,
        employeeId: null,
        // businessType: null,
        accidentCode: null,
        accidentName: null,
        accidentOccurTime: null,
        accidentEscalationTime: null,
        accidentDetails: null,
        score: null,
        accidentResult: null
        // postStatus: null,
        // createBy: null,
        // createTime: null,
        // updateBy: null,
        // updateTime: null,
        // delFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.disabledon = false
      this.open = true
      this.title = '新增事故'
    },
    /** 查看按钮操作 */
    handleexamine(row) {
      this.reset()
      const id = row.id || this.ids
      getAccident(id).then(response => {
        this.form = response.data
        this.disabledon = true
        this.open = true
        this.title = '查看事故'
      })
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      // const id = row.id || this.ids
      getAccident(row.id).then(response => {
        this.form = response.data
        this.disabledon = false
        this.open = true
        this.title = '修改事故'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // this.form.businessType = 1
            updateAccident(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            // this.form.businessType = 1
            addAccident(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除事故名称为"' + row.accidentName + '"的数据项？').then(function() {
        return delAccident(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/accident/export', {
        ...this.queryParams
      }, `accident_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style>
.el-checkbox__input.is-disabled + .el-checkbox__label {
  color: #606266 !important;
}

.el-checkbox__input.is-disabled.is-checked + .el-checkbox__label {
  color: #606266 !important;
}

.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner:after {
  border-color: #606266 !important;
}

.el-radio__input.is-disabled + span.el-radio__label {
  color: #606266 !important;
}

.el-radio__input.is-disabled.is-checked .el-radio__inner::after {
  background-color: #606266 !important;
}

.el-radio__input.is-disabled .el-radio__inner, .el-radio__input.is-disabled.is-checked .el-radio__inner {
  border-color: #606266 !important;
}

.el-input.is-disabled .el-input__inner, textarea:disabled {
  color: #606266 !important;
}

.el-range-editor.is-disabled input {
  color: #606266 !important;
}
.el-tooltip__popper{

  max-width:20%;

}

.el-tooltip__popper,.el-tooltip__popper.is-dark{

  background:rgb(48, 65, 86) !important;

  color: #fff !important;

  line-height: 24px;

}
.el-textarea__inner {
  font-family: 微软雅黑;
}
</style>
